home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / blix / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.7 KB  |  291 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*_________________________________________________________________________
  18.  |
  19.  | io.c - file opening, parsing and closing routines
  20.  |
  21.  | only one file can be open at any time; no check is made
  22.  | 
  23.  |     (c) 1993 Frans van Hoesel, Xtreme graphics software
  24.  |
  25. */
  26.  
  27.  
  28.  
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <bstring.h>
  35. #include <ctype.h>
  36. #include <stdarg.h>
  37. #include <gl/gl.h>
  38.  
  39. #include "config.h"
  40. #include "blixsound.h"
  41. #include "blix.h"
  42. #include "io.h"
  43.  
  44. /*_________________________________________________________________________
  45.  |
  46.  | static variables and constants used while reading the data files
  47.  | 
  48. */
  49.  
  50.  
  51. #define MAXLEN 256
  52. #ifndef UNCOMPRESS
  53. #define UNCOMPRESS "/usr/bsd/uncompress"
  54. #endif
  55.  
  56.  
  57. /* static variables */
  58. static char text[MAXLEN + 2];
  59. static char *chptr;
  60. static FILE *this_file = NULL;
  61. static char *filename;
  62. static int  linenum;
  63. static int piping;
  64.  
  65. void errormsg(const char *format, ...) {
  66.     va_list args;
  67.  
  68.     /* CONSTANTCONDITION */
  69.     va_start(args, format);
  70.     if (linenum == 0) {
  71.      fprintf(stderr, "%s: error: ", basename);
  72.     } else {
  73.     fprintf(stderr, "%s: error in file '%s' line %d: ", basename,
  74.             filename, linenum);
  75.     }
  76.     vfprintf(stderr, format, args);
  77.     fprintf(stderr, "\n");
  78.     va_end(args);
  79.     end_sound();
  80.     gexit();
  81.     exit (1);
  82. }
  83.  
  84. void warningmsg(const char *format, ...) {
  85.     va_list args;
  86.  
  87.     /* CONSTANTCONDITION */
  88.     va_start(args, format);
  89.     if (linenum == 0) {
  90.      fprintf(stderr, "%s: Warning: ", basename);
  91.     } else {
  92.     fprintf(stderr, "%s: Warning from file '%s' line %d: ", basename,
  93.             filename, linenum);
  94.     }
  95.     vfprintf(stderr, format, args);
  96.     fprintf(stderr, "\n");
  97.     va_end(args);
  98. }
  99.  
  100. char *lookahead(void) {
  101.     return chptr;
  102. }
  103. void open_file(const char *file_name) {
  104.  
  105.     char fullpath[MAXLEN * 2];
  106.     char pipecmd[MAXLEN * 2];
  107.     piping = FALSE;
  108.     linenum = 0;
  109.     filename = (char *) file_name;
  110.     strcpy(fullpath, datadir);
  111.     strcat(fullpath, filename);
  112.  
  113.     if (this_file != NULL) {
  114.     close_file();
  115.     }
  116.     this_file = fopen(fullpath,"r");
  117.     
  118.     if (this_file == NULL) {
  119.     /* perhaps the file is compressed ? */
  120.     strcat(fullpath, ".Z");
  121.         this_file = fopen(fullpath,"r");
  122.     if (this_file != NULL) {
  123.         fclose(this_file);
  124.         strcpy(pipecmd, UNCOMPRESS);
  125.         strcat(pipecmd, " -c ");
  126.         strcat(pipecmd, fullpath);
  127.         this_file = popen(pipecmd, "r");
  128.         if (this_file != NULL)
  129.         piping = TRUE;
  130.     }
  131.     }
  132.     if (this_file == NULL) {
  133.     errormsg("cannot open file '%s'", filename);
  134.     }
  135.     read_line(text);
  136.     chptr = text;
  137. }
  138.  
  139. void close_file(void) {
  140.     
  141.     if (piping) {
  142.     if (pclose(this_file)) {
  143.         errormsg("error while reading compressed data");
  144.     }
  145.     } else {
  146.     fclose(this_file);
  147.     }
  148.     this_file = NULL;
  149. }
  150.  
  151. int read_line(char *l) {
  152.     char    *r;
  153.  
  154.     do {
  155.     r = fgets(l, MAXLEN+1, this_file);
  156.     linenum++;
  157.     } while (r != NULL && (*l  == '\0' || *l == '#')) ;
  158.  
  159.     return (r != NULL);
  160. }
  161.  
  162. #pragma inline
  163. int skipblanks(void) {
  164. loop:
  165.     while (*chptr == ' ' || *chptr == '\t' || *chptr == '\n')
  166.     chptr++;
  167.     if (*chptr == '\0' || *chptr == '#') {
  168.     /* time to read the next line */
  169.     if (!read_line(text)) {
  170.         chptr = "end";
  171.         return *chptr;
  172.     }
  173.     chptr = text;
  174.     goto loop;
  175.     }
  176.     return *chptr;
  177. }
  178.  
  179. #define skipblanks_macro() \
  180.     while (*chptr == ' ' || *chptr == '\t' || *chptr == '\n') { \
  181.     chptr++; \
  182.     } \
  183.     while (*chptr == '\0' || *chptr == '#') { \
  184.     if (!read_line(text)) { \
  185.         chptr = "end"; \
  186.     } else {\
  187.         chptr = text; \
  188.         while (*chptr == ' ' || *chptr == '\t' ) { \
  189.         chptr++; \
  190.         } \
  191.     } \
  192.     } \
  193.  
  194.         
  195. void expect_ch(const char c) {
  196.     skipblanks_macro();
  197.     if ( *chptr == c)
  198.     chptr++;
  199.     else 
  200.     errormsg("'%c' expected", c);
  201. }
  202.  
  203. int test_ch(const char c) {
  204.     skipblanks_macro();
  205.     return (*chptr == c); 
  206. }
  207.  
  208. int test_str(const char *s) {
  209.     skipblanks_macro();
  210.     if (*chptr == *s) {
  211.     return (strncmp (s, chptr, strlen(s)) == 0);
  212.     } else {
  213.     return 0;
  214.     }
  215. }
  216.  
  217. int read_str(const char *s) {
  218.     if (test_str(s)) {
  219.     chptr += strlen(s);
  220.     return 0;
  221.     } else {
  222.     return 1;
  223.     }
  224. }
  225.  
  226. int read_ch(const char c) {
  227.     skipblanks_macro();
  228.     if (*chptr == c) {
  229.     chptr++;
  230.     return 0;
  231.     } else {
  232.     return 1;
  233.     }
  234.     
  235. }
  236. float accept_float(void) {
  237.  
  238.     char *ptr;
  239.     float f;
  240.  
  241.     skipblanks_macro();
  242.     f = strtod(chptr, &ptr);
  243.     if (chptr == ptr) 
  244.     errormsg("floating point number expected (%s)", chptr);
  245.     else
  246.     chptr = ptr;
  247.     return f;
  248. }
  249.  
  250.  
  251. int accept_int(void) {
  252.     
  253.     int s = 1;
  254.     int n;
  255.  
  256.     skipblanks_macro();
  257.     if (*chptr == '-') {
  258.     s = -1;
  259.     chptr++;
  260.     }
  261.     if (! isdigit(*chptr))
  262.     errormsg("digit expected");
  263.     n =  *chptr++ - '0';
  264.     while (isdigit (*chptr)) {
  265.     n = n * 10 + *chptr - '0';
  266.     chptr++;
  267.     }
  268.     return (s * n);
  269. }
  270.  
  271. char *accept_str(void) {
  272.     static char st[MAXLEN];
  273.     char *s;
  274.     
  275.     s = st;
  276.     skipblanks_macro();
  277.     while (isalnum(*chptr)) {
  278.     *s++ = *chptr++;
  279.     }
  280.     *s = '\0';
  281.     return st;    
  282. }
  283.  
  284. void skip_line(void) {
  285.     if (!read_line(text)) {
  286.     chptr = "end";
  287.     } else {
  288.     chptr = text;
  289.     }
  290. }
  291.